package com.taiyi.renaissancemall.ui.mine.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Guideline;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.MyScoresBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.ui.mine.wallet.transfer.TransferActivity;
import com.taiyi.renaissancemall.ui.mine.wallet.withdraw.WithdrawActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyWalletActivity extends BaseActivity<MyWalletView, MyWalletPresent> implements MyWalletView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.topB)
    TextView topB;
    @BindView(R.id.preSettlementTv)
    TextView preSettlementTv;
    @BindView(R.id.withDrawableTv)
    TextView withDrawableTv;
    @BindView(R.id.endorsementFeeTv)
    TextView endorsementFeeTv;
    @BindView(R.id.shoppingRewardsTv)
    TextView shoppingRewardsTv;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.balanceTv)
    TextView balanceTv;
    @BindView(R.id.ruleDescriptionTv)
    TextView ruleDescriptionTv;
    @BindView(R.id.guideLine)
    Guideline guideLine;
    @BindView(R.id.withdrawTv)
    TextView withdrawTv;
    @BindView(R.id.transferTv)
    TextView transferTv;
    @BindView(R.id.v)
    View v;
    @BindView(R.id.preSettlementListTv)
    TextView preSettlementListTv;
    @BindView(R.id.withdrawListTv)
    TextView withdrawListTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private String mType = "1";//1预结算礼券2可提现礼券6积分
    private int page = 1;
    private MyWalletListAdapter myWalletListAdapter;
    private List<MyScoresBean> data = new ArrayList<>();
    private String tel;
    private int withdrawStatus;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    public MyWalletPresent initPresenter() {
        return new MyWalletPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        withdrawStatus = getIntent().getIntExtra("toWithdrawSuccess", -1);
        titleTv.setText(getString(R.string.myWallet));
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        setAdapter(data, mType);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 0f).init();
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getMyWalletDetail();
        if (withdrawStatus == 1) {
            preSettlementListTv.setSelected(false);
            withdrawListTv.setSelected(true);
            presenter.getMyWalletList("2", page + "");
        } else {
            preSettlementListTv.setSelected(true);
            withdrawListTv.setSelected(false);
            presenter.getMyWalletList(mType, page + "");
        }

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getMyWalletSuccess(PersonInfoBean personInfoBean) {
        hideProgress();
        tel = personInfoBean.getTel();
        String numLock;
        if (personInfoBean.getLiquannumlock().contains(".0")) {
            double daiMoney = Double.parseDouble(personInfoBean.getLiquannumlock());
            String daiMoneyFormat = new DecimalFormat("#").format(daiMoney);
            numLock = getString(R.string.preSettlementGiftCertificate) + daiMoneyFormat;
        } else {
            numLock = getString(R.string.preSettlementGiftCertificate) + personInfoBean.getLiquannumlock();
        }
        String numTi;
        if (personInfoBean.getLiquannumti().contains(".0")) {
            double daiMoney = Double.parseDouble(personInfoBean.getLiquannumti());
            String daiMoneyFormat = new DecimalFormat("#").format(daiMoney);
            numTi = getString(R.string.withDrawable) + daiMoneyFormat;
        } else {
            numTi = getString(R.string.withDrawable) + personInfoBean.getLiquannumti();
        }
        double daiMoney = Double.parseDouble(personInfoBean.getDaiarea())
                + Double.parseDouble(personInfoBean.getDaibusnum())
                + Double.parseDouble(personInfoBean.getDaicomnum());
        String daiMoneyFormat = new DecimalFormat("0.0").format(daiMoney);
        String numEndorsementFee;
        if (daiMoneyFormat.contains(".0")) {
            double daiMoney1 = Double.parseDouble(daiMoneyFormat);
            String daiMoneyFormat1 = new DecimalFormat("#").format(daiMoney1);
            numEndorsementFee = getString(R.string.endorsementFee) + daiMoneyFormat1;
        } else {
            numEndorsementFee = getString(R.string.endorsementFee) + daiMoneyFormat;
        }
        String numShoppingRewards;
        if (personInfoBean.getPaycomnum().contains(".0")) {
            double daiMoney1 = Double.parseDouble(personInfoBean.getPaycomnum());
            String daiMoneyFormat1 = new DecimalFormat("#").format(daiMoney1);
            numShoppingRewards = getString(R.string.shoppingRewards) + daiMoneyFormat1;
        } else {
            numShoppingRewards = getString(R.string.shoppingRewards) + personInfoBean.getPaycomnum();
        }
        double balanceMoney = Double.parseDouble(personInfoBean.getLiquannumti())
                + Double.parseDouble(personInfoBean.getLiquannumlock());
        String decimalFormat = new DecimalFormat("0.0").format(balanceMoney);
        String numBalance;
        if (decimalFormat.contains(".0")) {
            double daiMoney1 = Double.parseDouble(decimalFormat);
            numBalance = new DecimalFormat("#").format(daiMoney1);
        } else {
            numBalance = decimalFormat;
        }

        preSettlementTv.setText(numLock);
        withDrawableTv.setText(numTi);
        endorsementFeeTv.setText(numEndorsementFee);
        shoppingRewardsTv.setText(numShoppingRewards);
        balanceTv.setText(numBalance);
    }

    @Override
    public void getMyWalletFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void getMyWalletListSuccess(List<MyScoresBean> myScoresBeans) {

        springView.onFinishFreshAndLoad();
        if (page == 1) {
            data.clear();
            if (myScoresBeans == null || myScoresBeans.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(myScoresBeans);
        if (myWalletListAdapter != null) {
            myWalletListAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void getMyWalletListFailure(String msg) {

    }

    private void setAdapter(List<MyScoresBean> dataBeanList, String type) {
        myWalletListAdapter = new MyWalletListAdapter(R.layout.mine_my_wallet_item_layout, dataBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyWalletActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myWalletListAdapter);
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getMyWalletList(mType, page + "");
    }

    @Override
    public void onLoadmore() {
        page += 1;
        presenter.getMyWalletList(mType, page + "");
    }

    @OnClick({R.id.backIv, R.id.transferTv, R.id.withdrawTv, R.id.preSettlementListTv, R.id.withdrawListTv, R.id.ruleDescriptionTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            //转账
            case R.id.transferTv:
                Intent intent_transfer = new Intent(MyWalletActivity.this, TransferActivity.class);
                startActivity(intent_transfer);
                break;
            //提现
            case R.id.withdrawTv:
                Intent intent_withdraw = new Intent(MyWalletActivity.this, WithdrawActivity.class);
                intent_withdraw.putExtra("tel", tel);
                startActivity(intent_withdraw);
                finish();
                break;
            //预结算列表
            case R.id.preSettlementListTv:
                preSettlementListTv.setSelected(true);
                withdrawListTv.setSelected(false);
                mType = "1";
                page = 1;
                presenter.getMyWalletList(mType, page + "");
                break;
            //可提现列表
            case R.id.withdrawListTv:
                preSettlementListTv.setSelected(false);
                withdrawListTv.setSelected(true);
                mType = "2";
                page = 1;
                presenter.getMyWalletList(mType, page + "");
                break;
            //规则说明
            case R.id.ruleDescriptionTv:
                break;
        }
    }

}
